var forumNameExistFlag = false;
var trNodeTag;
function checkForumNameCreateForum() {
	$(".requiredNotice").empty();
	var forumData = {
		"forumName" : $("#forumName").attr("value")
	};

	forumData = JSON2.stringify(forumData);
	var url = "/EKMS-web/forum/createForum.do?action=checkForumName";
	$.ajax( {
		url : url,
		data : forumData,
		type : "POST",
		processData : true,
		contentType : "application/json",
		timeout : 20000,
		dataType : "json",
		success : function(res) {
			checkForumNameSucc(res);
		},
		error : function() {
			$("#loadmessage").append("<br/>Error!Please refresh this page.");
		}
	});
}

function checkForumNameSucc(data) {
	$(".succTxt").show();
	$("#noticeTxt").append(data.notice);

	var forumNameExist = data.forum.forumNameExist;
	if (forumNameExist == "Yes") {
		$(".requiredNotice").show();
		$(".requiredNotice").append(
				'The "Forum Name" already exist, please fill in again!');
		forumNameExistFlag = true;
	} else if (forumNameExist == "No") {
		$(".requiredNotice").hide();
		forumNameExistFlag = false;
	}
}

function bindForumStatus() {
	var status = $("#hiddenForumNameQuery").val();
	$("#forumActiveQuery").val(status);
}

function searchForum() {
	document.frmModifyForum.action = "/EKMS-web/forum/modifyForum.do?action=listForum";
	document.frmModifyForum.submit();
}

function resetForumSearch() {
	$("#forumNameQuery").attr("value", "");
	$("#forumActiveQuery").val("");
}

function sortForum(eventSrc) {
	var type = eventSrc.title;
	$("#sortType").val(type);
	var order = $("#sortOrder").val();
	if (order == "asce") {
		sortOrder = "desc";
		$("#sortOrder").val("desc");
	} else {
		sortOrder = "asce";
		$("#sortOrder").val("asce");
	}
	document.frmModifyForum.action = "/EKMS-web/forum/modifyForum.do?action=listForum";
	document.frmModifyForum.submit();

}

function alertModifyForum(eventSrc){	
	var trNode = $(getParentTR(eventSrc));
	inputForumInfo(trNode);  
	showDiv();
	var forumData = {  
			"id":$("input[id='hiddenForumID']").attr("value"),
			"forumName":$("input[id='hiddenForumName']").attr("value")
	     };
	forumData = JSON2.stringify(forumData);
	var url ="../forum/modifyForum.do?action=initModifyForum";
	
	 $.ajax({
        url: url,
        data: forumData,
        type: "POST",
        processData: true,
        contentType: "application/json",
        timeout: 20000,
        dataType: "json"});		
}

function inputForumInfo(trNode){
	trNodeTag = trNode;
	var forumId = trNode.find("input[id='Id']").attr("value");
	var forumName = trNode.find("input[id='ForumName']").attr("value");
	var forumStatus =  trNode.find("input[id='ForumStatus']").attr("value");
	var forumDesc =  trNode.find("input[id='ForumDesc']").attr("value");
	
	$("#ckbActiveTopics").attr("checked",false);
	$("input[id='hiddenForumID']").attr("value",forumId);
	$("input[id='hiddenForumName']").attr("value",forumName);
	$("select[id='hiddenForumStatus']").attr("value",forumStatus);
	$("input[id='hiddenForumStatus']").attr("value",forumStatus);
	$("textarea[id='hiddenForumDesc']").attr("value",forumDesc);
	forumStatusChange();
}
function forumStatusChange(){
	var statusId =  $("select[id='hiddenForumStatus']").val();
	var status = $("select[id='hiddenForumStatus'] option[value='" + statusId + "']").attr("title");

	$("#ckbActiveTopics").attr("value",status).attr("checked","");
	$("#activeNotice").html(" Do you want to "+"\'"+status+"\'"+" all the topics under this forum?");
}
function goToPreviousPage(){
	var now = $("input[name='nowPage']").val();
	var total = $("input[name='totalPage']").val();
	if(parseInt(now) - 1 >= 1){
		document.frmModifyForum.action = "/EKMS-web/forum/modifyForum.do?action=goToPreviousPage";
		document.frmModifyForum.submit();
	}else{
		alert("This is the first page!");
	}
}

function goToPreviousPage(){
	var now = $("input[name='nowPage']").val();
	var total = $("input[name='totalPage']").val();
	if(parseInt(now) - 1 >= 1){
		document.frmModifyForum.action = "/EKMS-web/forum/modifyForum.do?action=goToPreviousPage";
		document.frmModifyForum.submit();
	}else{
		alert("This is the first page!");
	}
}

function goToNextPage(){
	var now = $("input[name='nowPage']").val();
	var total = $("input[name='totalPage']").val();
	if(parseInt(now) +1 <= total){
		document.frmModifyForum.action = "/EKMS-web/forum/modifyForum.do?action=goToNextPage";
		document.frmModifyForum.submit();
	}else{
		alert("This is the last page!");
	}
}

function goToLastPage(){
	var total = $("input[name='totalPage']").val();
	$("input[name='targetPage']").val(total);
	goToPage();
}

function goToPage(){
	var target = $("input[name='targetPage']").val();
	var total = $("input[name='totalPage']").val();
	if(typeof(target) != NaN && parseInt(target) > 0 && parseInt(target) <= parseInt(total) ){
		document.frmModifyForum.action = "/EKMS-web/forum/modifyForum.do?action=goToTargetPage";
		document.frmModifyForum.submit();
	}else{
		alert("The page number must be a number and between 0 and "+ total + "!");
	}
}

//Reset Forum Dialog
function resetForumDialog(){
	$("input").attr('disabled',false);
	$("textarea").attr('disabled',false);
	$("select").attr('disabled',false);
	$("#noticeTxt").empty();
	$(".succTxt").hide();
	$("#loadingPic").hide();
	$("#errors").hide();
	$("#noticeTxt").hide();
	closeDiv();
}

function  check(field,limit)
{
  if (field.value.length >= limit)
    {
        field.value = field.value.substr(0,limit-1);
        field.focus();
     }
}



var message;
function modifyForum(){
	$("#loadingPic").show();
	$("input").attr('disabled',' true');
	$("textarea").attr('disabled',' true');
	$("select").attr('disabled','true');
	$("#errors").empty();
	
	if($("#ckbActiveTopics").attr("checked")){
		isActiveOrInactiveTopic = $("#ckbActiveTopics").val();
	}else{
		isActiveOrInactiveTopic = "";
	}
	
	forumData = {  
			"id":$("input[id='hiddenForumID']").attr("value"),
			"forumName":$("input[id='hiddenForumName']").attr("value"),
			"status":$("select[id='hiddenForumStatus']").attr("value"),
			"isActiveOrInactiveTopic":isActiveOrInactiveTopic,
			"description":$("textarea[id='hiddenForumDesc']").attr("value")
	     };
		
	forumData = JSON2.stringify(forumData);
	var url ="../forum/modifyForum.do?action=modify";
	
	 $.ajax({
        url: url,
        data: forumData,
        type: "POST",
        processData: true,
        contentType: "application/json",
        timeout: 20000,
        dataType: "json",
        success: function(res){modifyForumSucc(res);} ,
        error: function(message) { $("#loadmessage").append(message+"<br/>Error!Please refresh this page."); }
    });
}
function modifyForumSucc(data){
	var res = data;
	message = data.notice;
	$("#noticeTxt").show();
	$("#noticeTxt").empty();
	$("#noticeTxt").append(data.notice);
	if(data.status == 0)
	{
		var forumId = data.forum.forumId;
		//$("#autoCloseMsg").show();
		var ids = $("input[title='forumId']");
		$.each(ids,function(i){
			if(ids[i].value == forumId){
				var trNode = $(getParentTR(ids[i]));
				trNode.find("input[id='Id']").attr("value",data.forum.forumId);
				trNode.find("input[id='ForumName']").attr("value",data.forum.forumName);
				trNode.find("input[id='ForumStatus']").attr("value",data.forum.active);
				trNode.find("input[id='ForumDesc']").attr("value",data.forum.description);
				trNode.find("span[id='ForumName']").html("<a onClick='alertModifyForum(this)' class='handLink'>" + data.forum.forumName + "</a>");
				if(data.forum.active == 0)
				{
					trNode.find("span[id='ForumStatus']").html("Yes");
				}else{
					trNode.find("span[id='ForumStatus']").html("No");
				}
				trNode.find("span[id='ForumDesc']").html(data.forum.description);
			}
		});
		$("#errors").hide();
		resetForumControls();
		//window.setTimeout(function(){resetForumDialog();},5000);
	}else{
		$("#errors").show();
		$("#noticeTxt").hide();
		var errors =  data.errors;
		$.each(errors,function(i){
			$("#errors").append(errors[i].error + "<br/>");
		});
		resetForumControls();
	}
}
function resetForumControls(){
	$("input").attr('disabled',false);
	$("textarea").attr('disabled',false);
	$("select").attr('disabled',false);
	$("#loadingPic").hide();
}

//back the data to the initi time
function resetMethod(){
	var forumId = trNodeTag.find("input[id='Id']").attr("value");
	var forumName = trNodeTag.find("input[id='ForumName']").attr("value");
	var forumStatus =  trNodeTag.find("input[id='ForumStatus']").attr("value");
	var forumDesc =  trNodeTag.find("input[id='ForumDesc']").attr("value");
	
	$("#ckbActiveTopics").attr("value",status).attr("checked","");
	$("input[id='hiddenForumID']").attr("value",forumId);
	$("input[id='hiddenForumName']").attr("value",forumName);
	$("select[id='hiddenForumStatus']").attr("value",forumStatus);
	$("input[id='hiddenForumStatus']").attr("value",forumStatus);
	$("textarea[id='hiddenForumDesc']").attr("value",forumDesc);
	$("#errors").hide();
	$("#noticeTxt").hide();
}
function resetForumDialog(){
	$("input").attr('disabled',false);
	$("textarea").attr('disabled',false);
	$("select").attr('disabled',false);
	$("#noticeTxt").empty();
	$(".succTxt").hide();
	$("#loadingPic").hide();
	$("#errors").hide();
	$("#noticeTxt").hide();
	closeDiv();
}




